package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import net.pinnoocle.playdao.dao.MasterDao;
import net.pinnoocle.playdao.dao.MasterScriptDao;
import net.pinnoocle.playdao.entity.MasterScript;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 主持人会的剧本(MasterScript)表服务实现类
 *
 * @author ye
 * @since 2021-10-28 17:31:01
 */
@Service("masterScriptService")
@Slf4j
public class MasterScriptServiceImpl extends ServiceImpl<MasterScriptDao, MasterScript> {

    @Autowired
    MasterDao masterDao;

    /**
     * 主持人添加会的本
     * @param masterId
     * @param shopId
     * @param scriptIds
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
     public boolean addMasterScripts(String masterId, String shopId, List<String> scriptIds){
         try {
             masterDao.batchDeleteMasterScript(masterId);
             ArrayList<MasterScript> masterScripts = new ArrayList<>();
             for (String scriptId : scriptIds){
                 MasterScript masterScriptDetail = masterDao.masterScriptDetail(masterId, scriptId);
                 if(Objects.nonNull(masterScriptDetail)){
                     masterScriptDetail.setIsDeleted(0);
                     this.updateById(masterScriptDetail);
                 }else{
                     MasterScript masterScript = new MasterScript();
                     masterScript.setMasterId(masterId);
                     masterScript.setShopId(shopId);
                     masterScript.setScriptId(scriptId);
                     masterScript.setCreatedTime(new Date());
                     masterScripts.add(masterScript);
                 }
             }
             return this.saveBatch(masterScripts);
         } catch (Exception e) {
             log.info("添加主持人剧本失败："+e.getMessage());
         }
         return false;
     }
}